//
//  SDImageView.m
//  Pods
//
//  Created by molon on 15/7/14.
//
//

#import "SDImageView.h"
#import <UIImageView+WebCache.h>

@implementation SDImageView

- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock {
    if ([url isFileURL]) {
        //TODO:这个没有存imageURL
        [self sd_cancelCurrentImageLoad];
        
        //下面的这些不能放置到多线程，因为可能会影响到重用，因为sd_cancelCurrentImageLoad，cancel不了下面的处理，所以可能最终setImage是不理想的
        if (!(options & SDWebImageDelayPlaceholder)) {
            self.image = placeholder;
        }
        
        //直接读文件
        UIImage *image = [UIImage imageWithContentsOfFile:[url path]];
        if (image && (options & SDWebImageAvoidAutoSetImage) && completedBlock)
        {
            completedBlock(image, nil, SDImageCacheTypeDisk, url);
            return;
        }else if (image) {
            self.image = image;
            [self setNeedsLayout];
        } else {
            if ((options & SDWebImageDelayPlaceholder)) {
                self.image = placeholder;
                [self setNeedsLayout];
            }
        }
        if (completedBlock) {
            completedBlock(image, nil, SDImageCacheTypeDisk, url);
        }
        return;
    }
    
    [super sd_setImageWithURL:url placeholderImage:placeholder options:options|SDWebImageRetryFailed progress:progressBlock completed:completedBlock];
}

@end
